﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;


public partial class cn_UserControl_header : System.Web.UI.UserControl
{
    // Fields
    protected string basicSetXmlPath = "/cn/xml/website_set.xml";
    tbKeyWords oKeywords = new tbKeyWords();
    readonly DALKeyWords theKeywords = new DALKeyWords();
    protected string moduleSetXmlPath = "/cn/xml/module_set.xml";
    BasePage thePage = new BasePage();

    // Methods
    protected StringBuilder BrandBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.moduleSetXmlPath));
            XmlElement element = document.SelectSingleNode("set/top_brand") as XmlElement;
            if (!(element.GetAttribute("isShow") == "true"))
            {
                return builder;
            }
            DataTable brandByCompanyId = BaseFactory.theBrand.GetBrandByCompanyId(thePage.CompanyId);
            builder.AppendLine("<div class=\"brand\">");
            builder.AppendLine("<div>");
            builder.AppendLine("<h6>" + element.GetAttribute("title") + "：</h6>");
            foreach (DataRow row in brandByCompanyId.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<a href=\"", row["brandUrl"], "\"><img src=\"", row["brandLogo"], "\" alt=\"", row["brandName"], "\" /></a>" }));
            }
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder KeyWordsBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            builder.AppendLine("<p>");
            builder.AppendLine("<strong>热门搜索：</strong>");
            DataTable keyWordsByTopAndIsShow = this.theKeywords.GetKeyWordsByTopAndIsShow(10, 1);
            foreach (DataRow row in keyWordsByTopAndIsShow.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<a href=\"/cn/goods_list.html?kw=", HttpUtility.UrlEncode(row["keyWord"].ToString()), "\" class=\"red\">", row["keyWord"], "</a>" }));
            }
            builder.AppendLine("</p>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder MenuNavigationBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table3;
            DataTable brandByCategoryIdentity;
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.moduleSetXmlPath));
            XmlElement element = document.SelectSingleNode("set/navigation_brand") as XmlElement;
            DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow("00000000-0000-0000-0000-000000000000", thePage.CompanyId, 1);
            DataTable table2 = BaseFactory.theCategory.GetCategoryByNavigationTypeAndIsShow(0, 1, thePage.CompanyId);
            builder.AppendLine("<div class=\"menu\">");
            builder.AppendLine("<div class=\"hcategory2\" id=\"categorybox\">");
            builder.AppendLine("<div class=\"navin clearfix\">");
            builder.AppendLine("<div class=\"search_bar clearfix\">");
            builder.AppendLine("<div class=\"hcategory2\" id=\"categorybox2\">");
            builder.AppendLine("<div class=\"cate-tit\" id=\"cate_tit\"><a href=\"/cn/goods_list.html\" title=\"全部商品分类\"><span>全部商品分类</span></a></div>");

            if (!Request.Url.ToString().Contains("index.html") && !Request.Url.ToString().Contains("index.aspx"))
            {
                builder.AppendLine("<div class=\"category_bar\" id=\"category_bar\">");
                builder.AppendLine("<div class=\"cate-tb\"></div>");
                builder.AppendLine("<div id=\"cate_bar\">");
                int num = 1;
                foreach (DataRow row in table.Rows)
                {
                    builder.AppendLine("<div class=\"cate_item\">");
                    builder.AppendLine("<div class=\"cate_title\">");
                    builder.AppendLine(string.Concat(new object[] { "<h3><strong><a href=\"/cn/goods_list.html?cid=", row["id"], "\">", row["categoryName"], "</a></strong></h3>" }));
                    builder.AppendLine("<em></em>");
                    builder.AppendLine("</div>");
                    builder.AppendLine("<div class=\"citem top_" + num + "\">");
                    builder.AppendLine("<div class=\"citem_left\">");
                    builder.AppendLine("<div class=\"all_cate_tit\">全部分类</div>");
                    builder.AppendLine("<dl>");
                    table3 = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow(row["id"].ToString(), thePage.CompanyId, 1);
                    foreach (DataRow row2 in table3.Rows)
                    {
                        builder.AppendLine(string.Concat(new object[] { "<dt><a href=\"goods_list.html?cid=", row2["categoryIdentity"], "\">", row2["categoryName"], "</a> </dt>" }));
                        DataTable table4 = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow(row2["id"].ToString(), thePage.CompanyId, 1);
                        foreach (DataRow row3 in table4.Rows)
                        {
                            builder.AppendLine(string.Concat(new object[] { "<dd><a href=\"/cn/goods_list.html?cid=", row3["categoryIdentity"], "\">", row3["categoryName"], "</a></dd>" }));
                        }
                    }
                    builder.AppendLine("</dl>");
                    builder.AppendLine("</div>");
                    if (element.GetAttribute("isShow") == "true")
                    {
                        builder.AppendLine("<div class=\"citem_right\">");
                        builder.AppendLine("<dl class=\"brands\">");
                        builder.AppendLine("<dt>" + element.GetAttribute("title") + "</dt>");
                        builder.AppendLine("<dd>");
                        brandByCategoryIdentity = BaseFactory.theBrand.GetBrandByCategoryIdentity(Convert.ToInt32(row["categoryIdentity"]));
                        foreach (DataRow row4 in brandByCategoryIdentity.Rows)
                        {
                            builder.AppendLine(string.Concat(new object[] { "<em><a href=\"/cn/goods_list.html?cid=", row["categoryIdentity"], "&bid=", row4["id"], "\">", row4["brandName"], "</a></em>" }));
                        }
                        builder.AppendLine("</dd>");
                        builder.AppendLine("</dl>");
                        builder.AppendLine("</div>");
                    }
                    builder.AppendLine("<span class=\"clear\"></span>");
                    builder.AppendLine("</div>");
                    builder.AppendLine("</div>");
                    num++;
                }
                builder.AppendLine("<div class=\"allcate\"><a href=\"/cn/goods_list.html\">全部商品分类</a></div>");
                builder.AppendLine("<div class=\"catebom\"></div>");
                builder.AppendLine("</div>");
                builder.AppendLine("</div>");
            }
         
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("<ul id=\"navMenu\">");
            builder.AppendLine("<li><a href=\"/cn/index.html\" class=\"home\">首页</a></li>");
            foreach (DataRow row in table2.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<li><a href=\"/cn/goods_list.html?cid=", row["id"], "\" target=\"_blank\" ", row["categoryIdentity"], "\">", row["categoryName"], "</a></li>" }));
            }
            DataTable allCustomNavigation = BaseFactory.theCustomNavigation.GetList("").Tables[0];
            foreach (DataRow row in allCustomNavigation.Rows)
            {
                //builder.AppendLine(string.Concat(new object[] { "<li><a href=\"", row["url"], ">", row["title"], "</a></li>" }));
                builder.AppendLine(string.Concat(new object[] { "<li><a  href=\"", row["url"], "\">", row["title"], "</a></li>" }));
            }
            builder.AppendLine("<li><a class=\"thickbox last\" href=\"/cn/exchange.aspx?TB_iframe=true&height=370&width=640\" target=\"_blank\";\">我要提货</a></li>");
            builder.AppendLine("</ul>");
            //foreach (DataRow row5 in table2.Rows)
            //{
            //    brandByCategoryIdentity = BaseFactory.theBrand.GetBrandByCategoryIdentity(Convert.ToInt32(row5["categoryIdentity"]));
            //    builder.AppendLine("<div id=\"dropmenu" + row5["categoryIdentity"] + "\" class=\"dropMenu\">");
            //    builder.AppendLine("<dl>");
            //    table3 = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow(row5["id"].ToString(), thePage.CompanyId, 1);
            //    foreach (DataRow row2 in table3.Rows)
            //    {
            //        builder.AppendLine(string.Concat(new object[] { "<dt><a href=\"/cn/goods_list.html?cid=", row2["categoryIdentity"], "\" class=\"title_menu\">", row2["categoryName"], "</a></dt>" }));
            //        DataTable table7 = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow(row2["id"].ToString(), thePage.CompanyId, 1);
            //        if (table7.Rows.Count > 0)
            //        {
            //            builder.AppendLine("<dd>");
            //            for (int i = 0; i < table7.Rows.Count; i++)
            //            {
            //                builder.AppendLine(string.Concat(new object[] { "<a href=\"/cn/goods_list.html?cid=", table7.Rows[i]["categoryIdentity"], "\" >", table7.Rows[i]["categoryName"], "</a>" }));
            //            }
            //            builder.AppendLine("</dd>");
            //        }
            //        else
            //        {
            //            builder.AppendLine("<dd style=\"height:1px; overflow:hidden;\"></dd>");
            //        }
            //    }
            //    builder.AppendLine("</dl>");
            //    if (element.GetAttribute("isShow") == "true")
            //    {
            //        builder.AppendLine("<div class=\"brand_menu\">");
            //        builder.AppendLine("<p><strong>" + element.GetAttribute("title") + "</strong></p>");
            //        foreach (DataRow row4 in brandByCategoryIdentity.Rows)
            //        {
            //            builder.AppendLine(string.Concat(new object[] { "<p><a href=\"/cn/goods_list.html?cid=", row5["categoryIdentity"], "&bid=", row4["id"], "\">", row4["brandName"], "</a></p>" }));
            //        }
            //        builder.AppendLine("</div>");
            //    }
            //    builder.AppendLine("</div>");
            //}
            builder.AppendLine("<div class=\"clear\"></div>");
            builder.AppendFormat("<div class=\"card_info\">{0}</div>", GetMemberCardInfo());
            builder.AppendLine("<div class=\"clear\"></div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(base.IsPostBack || (base.Request.QueryString["kw"] == null)))
        {
            this.oKeywords.keyWord = base.Request.QueryString["kw"];
            this.theKeywords.AddKeyWords(this.oKeywords);
        }
    }

    protected StringBuilder UserBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            string attribute = "";
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.basicSetXmlPath));
            attribute = ((XmlElement)document.SelectSingleNode("set/cn_enterprise_sort_name")).GetAttribute("value");
            builder.AppendLine("<div class=\"wel\">");
            builder.AppendLine("<div class=\"width1000\">");
            builder.AppendLine("<p class=\"top_function\">");
            if (thePage.LoginUserMember == null || thePage.LoginUserMember.ClientUser == null)
            {
                builder.AppendLine("<a  class=\"thickbox order\" href=\"/cn/login.aspx?TB_iframe=true&height=370&width=640\" target=\"_blank\">我的订单</a>");
            }
            else
            {
                builder.AppendLine("<a href=\"/cn/member_center/my_order.html\" class=\"order\">我的订单</a>");
            }
            builder.AppendLine("<a href=\"/cn/shopping_cart.html\" class=\"shopping_car\">我的购物车</a><a href=\"/cn/help_center/help_list.html\" class=\"help\">帮助中心</a><a href=\"/cn/news_center/news_list.html\" class=\"into\">资讯中心</a><a href=\"/cn/recruitment/recruitment_list.html\" class=\"human\">人才招聘</a><a href=\"/cn/online_message.html\" class=\"omessage\">在线留言</a>");
            builder.AppendLine("<p id=\"puser\">");
            if (base.Session["user"] != null)
            {
                int inBoxNoIsReadByAddressee = BaseFactory.theInBox.GetInBoxNoIsReadByAddressee((base.Session["user"] as tbUser).userName);
                builder.AppendLine(string.Concat(new object[] { "您好，<span class=\"red\">", (base.Session["user"] as tbUser).userName, "</span>，欢迎光临 <strong>", attribute, "</strong>！&nbsp;<a href=\"/cn/member_center/my_order.html\">[&nbsp;会员中心&nbsp;]</a>&nbsp;<a class=\"mail\" href=\"/cn/member_center/in_box.html\">(<strong>", inBoxNoIsReadByAddressee, "</strong>)</a>&nbsp;<a href=\"userExitHandler.ashx\">[&nbsp;退出&nbsp;]</a>" }));
            }
            else
            {
                builder.AppendLine("您好，欢迎光临 <strong>" + attribute + "</strong>！&nbsp;<a href=\"/cn/login.html?TB_iframe=true&height=370&width=640\" class=\"thickbox\">[<span class=\"red\">登录</span></a> /&nbsp;<a href=\"/cn/register.html?TB_iframe=true&height=370&width=640\" class=\"thickbox\">注册]</a>");
            }
            builder.AppendLine("<p>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

    public string GetMemberCardInfo()
    {

        if (thePage.LoginUserMember != null)
            {
                if (thePage.LoginUserMember.DicUserCardList != null && thePage.LoginUserMember.DicUserCardList.Count > 0)
                {
                    StringBuilder sbInfo = new StringBuilder("您的卡券详情 ");
                    foreach (tbCard card in thePage.LoginUserMember.DicUserCardList.Values)
                    {

                        sbInfo.AppendFormat(" *卡号:{0},卡名称:{1},卡面值:{2}, <input id=\"removeCard\" class=\"submitCardInfo\" myvalue=\"{3}\" type=\"submit\" value=\"[删除]\" />"
                                              , card.cardcode
                                              , card.CardTypeName
                                              , card.cardprice.ToString("#,##")
                                              , card.cardid);
                    }
                    return sbInfo.ToString() + "&nbsp;<a class=\"thickbox\" href=\"/cn/exchange.aspx?TB_iframe=true&height=370&width=640\" target=\"_blank\">[继续输入卡券];</a>"; 
                }

            }
            return string.Empty;

    }

}